package com.mickey.controller;

import com.mickey.domain.MiaoshaUser;
import com.mickey.redis.GoodsKey;
import com.mickey.redis.RedisService;
import com.mickey.result.Result;
import com.mickey.service.GoodsService;
import com.mickey.service.MiaoshaUserService;
import com.mickey.vo.GoodsDetailVO;
import com.mickey.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private MiaoshaUserService miaoshaService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    ApplicationContext applicationContext;

    @RequestMapping(value = "/to_list",produces = "text/html")
    @ResponseBody
    public String toGoodsList(HttpServletRequest request, HttpServletResponse response,Model mode, MiaoshaUser user){
        //取缓存
        String html = redisService.get(GoodsKey.getGoodsList,"",String.class);
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        mode.addAttribute("user",user);
        List<GoodsVO> goodsVOList = goodsService.listGoodsVO();
        mode.addAttribute("goodsList",goodsVOList);

        //手动渲染
        SpringWebContext ctx = new SpringWebContext(request,response,request.getServletContext(),
                request.getLocale(),mode.asMap(),applicationContext);
        html = thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set(GoodsKey.getGoodsList,"",html);
        }
        return html;
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVO> detail(HttpServletRequest request, HttpServletResponse response, Model mode, MiaoshaUser user,
                                           @PathVariable("goodsId")long goodsId){
        GoodsVO goods = goodsService.getGoodsVOByGoodsId(goodsId);
        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();
        int miaoshaStatus = 0;
        int remainSeconds = 0;
        if(now < startAt){//秒杀还没开始
            miaoshaStatus = 0;
            remainSeconds = (int) ((startAt - now)/1000);
        }else if (now > endAt){//秒杀已经结束
            miaoshaStatus = 2;
            remainSeconds = -1;
        }else{//秒杀中
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO();
        goodsDetailVO.setGoods(goods);
        goodsDetailVO.setRemainSeconds(remainSeconds);
        goodsDetailVO.setMiaoshaStatus(miaoshaStatus);
        return Result.sucess(goodsDetailVO);
    }



    @RequestMapping(value = "/to_detail2/{goodsId}",produces = "text/html")
    @ResponseBody
    public String to_detail2(HttpServletRequest request, HttpServletResponse response,Model mode, MiaoshaUser user,
                            @PathVariable("goodsId")long goodsId){
        //取缓存
        String html = redisService.get(GoodsKey.getGoodsDetail,"" + goodsId,String.class);
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        mode.addAttribute("user",user);
        GoodsVO goods = goodsService.getGoodsVOByGoodsId(goodsId);
        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();
        int miaoshaStatus = 0;
        int remainSeconds = 0;
        if(now < startAt){//秒杀还没开始
            miaoshaStatus = 0;
            remainSeconds = (int) ((startAt - now)/1000);
        }else if (now > endAt){//秒杀已经结束
            miaoshaStatus = 2;
            remainSeconds = -1;
        }else{//秒杀中
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        mode.addAttribute("goods",goods);
        mode.addAttribute("miaoshaStatus",miaoshaStatus);
        mode.addAttribute("remainSeconds",remainSeconds);
        //手动渲染
        SpringWebContext ctx = new SpringWebContext(request,response,request.getServletContext(),
                request.getLocale(),mode.asMap(),applicationContext);
        html = thymeleafViewResolver.getTemplateEngine().process("goods_detail",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set(GoodsKey.getGoodsDetail,"" + goodsId,html);
        }
        return html;
    }
}
